package main

import (
	"database/sql"
	"flag"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/golang-migrate/migrate"
	_ "github.com/golang-migrate/migrate/database"
	"github.com/golang-migrate/migrate/database/mysql"
	_ "github.com/golang-migrate/migrate/database/mysql"
	_ "github.com/golang-migrate/migrate/source/file"
	_ "github.com/golang-migrate/migrate/source/github"
	"log"
)

func main() {
	var migrationDir = flag.String("migration.files", "./migrations", "Directory where the migration files are located ?")
	db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/migrate_demo?multiStatements=true")

	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	driver, err := mysql.WithInstance(db, &mysql.Config{})

	if err != nil {
		log.Fatal(err)
	}

	m, err := migrate.NewWithDatabaseInstance(
		fmt.Sprintf("file://%s", *migrationDir),
		"migrate_demo",
		driver,
	)
	if err != nil {
		log.Fatal(err)
	}
	//or m.Down()
	err = m.Up()
	//err = m.Down()
	if err != nil {
		log.Fatal(err)
	}
	//执行的文件数
	//_ = m.Steps(1)
}
